package com.ruoyi.erp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.erp.domain.bo.InventoryDetailBo;
import com.ruoyi.erp.domain.vo.InventoryDetailVo;
import com.ruoyi.erp.domain.InventoryDetail;
import com.ruoyi.erp.mapper.InventoryDetailMapper;
import com.ruoyi.erp.service.IInventoryDetailService;

import java.util.List;
import java.util.Collection;

/**
 * 商品扩展Service业务层处理
 *
 * @author chenyun
 * @date 2022-03-13
 */
@Service
public class InventoryDetailServiceImpl extends ServicePlusImpl<InventoryDetailMapper, InventoryDetail, InventoryDetailVo> implements IInventoryDetailService {
    private static final String LOCK_KEY = "InventoryDetail";
    @Override
    public InventoryDetailVo queryById(Long id){
        return getVoById(id);
    }

    @Override
    public TableDataInfo<InventoryDetailVo> queryPageList(InventoryDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<InventoryDetail> lqw = buildQueryWrapper(bo);
        Page<InventoryDetailVo> result = pageVo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public List<InventoryDetailVo> queryList(InventoryDetailBo bo) {
        LambdaQueryWrapper<InventoryDetail> lqw = buildQueryWrapper(bo);
        return listVo(lqw);
    }

    @Override
    public Boolean insertByBo(InventoryDetailBo bo) throws Exception{
        InventoryDetail add = BeanUtil.toBean(bo, InventoryDetail.class);
        validEntityBeforeSave(add);
        QueryWrapper wrapper = existQueryWrapper(add);
        boolean flag = insertIdempotency(add,LOCK_KEY,wrapper,getMsg(add));
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(InventoryDetailBo bo) throws Exception{
        InventoryDetail update = BeanUtil.toBean(bo, InventoryDetail.class);
        validEntityBeforeSave(update);
        QueryWrapper wrapper = existQueryWrapper(update);
        boolean flag = updateIdempotency(update,LOCK_KEY,wrapper,getMsg(update));
        return flag;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(InventoryDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    /**
     * 唯一判断查询条件，暂支持一个键
     * @param entity
     * @return
     */
    private QueryWrapper<InventoryDetail> existQueryWrapper(InventoryDetail entity) {
        return null;
    }

    private String getMsg(InventoryDetail entity){
            return null;
    }
}
